package com.ruoyi.project.mall.service.impl;


import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.bootdo.PageHelperUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.mall.dao.MallInvoiceUserDao;
import com.ruoyi.project.mall.domain.MallInvoiceUserDO;
import com.ruoyi.project.mall.service.MallInvoiceUserService;
import com.ruoyi.project.wx.dao.OrderOrderDao;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class MallInvoiceUserServiceImpl implements MallInvoiceUserService {
	@Autowired
	private MallInvoiceUserDao mallInvoiceUserDao;
	@Autowired
	private OrderOrderDao orderOrderDao;
	
	@Override
	public MallInvoiceUserDO get(String id){
		return mallInvoiceUserDao.get(id);
	}
	
	@Override
	public List<MallInvoiceUserDO> list(Map<String, Object> map){
		return mallInvoiceUserDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return mallInvoiceUserDao.count(map);
	}
	
	@Override
	public int save(MallInvoiceUserDO mallInvoiceUser){
		return mallInvoiceUserDao.save(mallInvoiceUser);
	}
	
	@Override
	public int update(MallInvoiceUserDO mallInvoiceUser){
		return mallInvoiceUserDao.update(mallInvoiceUser);
	}
	
	@Override
	public int remove(String id){
		return mallInvoiceUserDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return mallInvoiceUserDao.batchRemove(ids);
	}

    @Override
    public List<MallInvoiceUserDO> likeList(Map<String, Object> map){
        return mallInvoiceUserDao.likeList(map);
    }
    @Override
    public int likeListCount(Map<String, Object> map){
        return mallInvoiceUserDao.likeListCount(map);
    }

    @Override
    public R saveUserInvoice(Map<String, Object> paramMap) {
		try {
			MallInvoiceUserDO mallInvoiceUserDO = new MallInvoiceUserDO();
			BeanUtils.populate(mallInvoiceUserDO,paramMap);
			mallInvoiceUserDO.setDelFlag(0);
			mallInvoiceUserDO.setUpAt(new Date());
			mallInvoiceUserDO.setCaAt(new Date());
			mallInvoiceUserDO.setId(UuidUtil.get32UUID());
			Map<String,Object> map = new HashMap<>();
			map.put("userId",paramMap.get("userId").toString());
			map.put("delFlag","0");
			map.put("idDefault","1");

			List<MallInvoiceUserDO> list = mallInvoiceUserDao.list(map);
			if(list.size()<=0){
				mallInvoiceUserDO.setIdDefault(1);
			}else{
				mallInvoiceUserDO.setIdDefault(0);
			}
			mallInvoiceUserDao.save(mallInvoiceUserDO);
			return R.ok(0,"添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("添加失败");
		}


	}

	@Override
	public R getUserInvoiceList(Map<String, Object> paramMap) {

		paramMap.put("delFlag","0");
		paramMap.put("sort","ca_at");
		paramMap.put("order","desc");
		PageHelperUtils.page(paramMap);
		List<MallInvoiceUserDO> list = mallInvoiceUserDao.list(paramMap);
		PageInfo<MallInvoiceUserDO> pageInfo = new PageInfo<>(list);
		return R.ok().put("data",pageInfo);
	}

	@Override
	public R setUserInvoiceDefault(Map<String, Object> paramMap) {
		Map<String,Object> map = new HashMap<>();
		map.put("userId",paramMap.get("userId").toString());
		map.put("delFlag","0");
		map.put("idDefault","1");

		List<MallInvoiceUserDO> list = mallInvoiceUserDao.list(map);
		if(list.size()>0){
			for (MallInvoiceUserDO mallInvoiceUserDO : list) {
				mallInvoiceUserDO.setIdDefault(0);
				mallInvoiceUserDO.setUpAt(new Date());
				mallInvoiceUserDao.update(mallInvoiceUserDO);
			}
		}
		MallInvoiceUserDO mallInvoiceUserDO = mallInvoiceUserDao.get(paramMap.get("invoiceId").toString());
		mallInvoiceUserDO.setIdDefault(1);
		mallInvoiceUserDO.setUpAt(new Date());
		mallInvoiceUserDao.update(mallInvoiceUserDO);
		return R.ok();
	}

	@Override
	public R deleteUserInvoice(Map<String, Object> paramMap) {
		MallInvoiceUserDO mallInvoiceUserDO = mallInvoiceUserDao.get(paramMap.get("invoiceId").toString());

		mallInvoiceUserDO.setDelFlag(1);
		mallInvoiceUserDO.setUpAt(new Date());
		mallInvoiceUserDao.update(mallInvoiceUserDO);
		return R.ok(0,"删除成功");
	}

    @Override
    public R getOrderInvoiceList(Map<String, Object> paramMap) {
		PageHelperUtils.page(paramMap);
		List<Map<String,Object>> mapList = orderOrderDao.getOrderInvoiceList(paramMap);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(mapList);
		return R.ok().put("data",pageInfo);
    }

    @Override
    public R getDefaultInvoice(Map<String, Object> paramMap) {
		Map<String,Object> map = mallInvoiceUserDao.getDefaultInvoice(paramMap);
		return R.ok().put("data",map);


    }


}
